%本代码的主要作用是绘制特定子轨迹，计算相应的平均偏转角


clc
clear
currentDir = pwd; % 获取当前目录路径
parentDir = fileparts(currentDir); % 获取当前目录的上一级目录路径
% grandParentDir = fileparts(parentDir); % 获取当前目录的上两级目录路径
% greatGrandParentDir = fileparts(grandParentDir); % 获取当前目录的上三级目录路径

%指定需要查找的文件夹
subdir = 'DATA';
%指定需要查找的文件名
filename = '413168000(NoRepeated).mat';%该数据为原始数据剔除重复值后的数据
%构造需要查找的文件的完整路径
full_path = fullfile(parentDir, subdir, filename);
current_data = load(full_path);%当前船舶的ais数据
current_data = current_data.data;
% current_data (current_data.Destination ~='CJK',:)=[];
ship_AIS =current_data(:,{'Lon','Lat'});


index_error=439;

window_size_bf = 3;%前窗口大小
window_size_af = 4;

data_mid_org=current_data([438,439,440,441],:);%原始异常数据集
data_bf = current_data(438-window_size_bf:438-1,:);
data_af=current_data(441+1:441+window_size_af,:);

data_org = [data_bf;data_mid_org;data_af];
data_mid_deleted=data_mid_org([1,3,4],:);%剔除异常数据后的异常数据集
data_deleted=[data_bf;data_mid_deleted;data_af]; %剔除异常数据集后的边界数据集

time_diff = data_deleted.time-data_deleted.time(1);
time_interp = current_data.time(index_error)-data_deleted.time(1);
data_error_special = current_data(index_error,:);%针对当前特定的异常点
data_error_special.Lon = spline(time_diff,data_deleted.Lon,time_interp);
data_error_special.Lat = spline(time_diff,data_deleted.Lat,time_interp);

data_mid_spline=[data_mid_deleted(1,:);data_error_special;data_mid_deleted(2:end,:)];%插值后的异常数据集

data_spline=[data_deleted(1:4,:);data_error_special;data_deleted(5:end,:)];



figure
subplot(3,1,1)
plot(data_org.Lon,data_org.Lat,'b*-');
hold on 
plot(data_org.Lon(5),data_org.Lat(5),'ro');
subplot(3,1,2)
plot(data_deleted.Lon,data_deleted.Lat,'b*-');
subplot(3,1,3)
plot(data_spline.Lon,data_spline.Lat,'b*-');
hold on 
plot(data_spline.Lon(5),data_spline.Lat(5),'ro');


%计算平均偏转角度=偏转角度和/偏转次数

degrees_mid_org=calculate_track_degree([data_org.Lon,data_org.Lat]);
degrees_org_mean=mean(degrees_mid_org);

degrees_mid_deleted=calculate_track_degree([data_deleted.Lon,data_deleted.Lat]);
degrees_deleted_mean=mean(degrees_mid_deleted);

degrees_mid_spline=calculate_track_degree([data_spline.Lon,data_spline.Lat]);
degrees_spline_mean=mean(degrees_mid_spline);



